package me.firstapp.service.shop.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.utils.Page;
import me.firstapp.common.utils.StrUtils;
import me.firstapp.module.shop.Product;
import me.firstapp.module.shop.ProductSkuRelation;
import me.firstapp.module.shop.Sku;
import me.firstapp.repository.shop.ProductRepository;
import me.firstapp.repository.shop.ProductSkuRelationRepository;
import me.firstapp.repository.shop.SkuRepository;
import me.firstapp.service.shop.ProductSkuRelationService;

@Service
public class ProductSkuRelationServiceImpl implements ProductSkuRelationService {

	@Autowired
	private ProductRepository productRepository;

	@Autowired
	private SkuRepository skuRepository;

	@Autowired
	private ProductSkuRelationRepository productSkuRelationRepository;

	@Override
	public ProductSkuRelation addProductSkuRelation(Long productId, String skuName1, String skuValue1, String skuName2,
			String skuValue2, Integer stock, Double price, Long apiClientId) {
		if (productId == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "商品id");
		}
		if (stock == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "库存");
		}
		if (price == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "价格");
		}
		if (StrUtils.isNULL(skuName1)) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "商品属性1");
		}
		Product product = productRepository.findProductById(productId, apiClientId);
		// 商品是否存在
		if (product == null) {
			throw new ServiceException(StatusHouse.PRODUCT_NOT_EXIST);
		}
		// 商品是否已上架
		if (product.getDisable() == 1) {
			throw new ServiceException(StatusHouse.PRDT_IS_SALLING);
		}
		if (StrUtils.isNotNULL(skuName1)) {
			Sku sku = skuRepository.findSkuByProductIdAndSkuName(productId, skuName1, apiClientId);
			// 属性1是否存在
			if (sku == null) {
				throw new ServiceException(StatusHouse.SKU_NOT_FOUND);
			}
			// 属性值1是否为空
			if (StrUtils.isNULL(skuValue1)) {
				throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "属性值1");
			}
		}
		if (StrUtils.isNotNULL(skuName2)) {
			Sku sku = skuRepository.findSkuByProductIdAndSkuName(productId, skuName2, apiClientId);
			// 属性2是否存在
			if (sku == null) {
				throw new ServiceException(StatusHouse.SKU_NOT_FOUND);
			}
			// 属性值2是否为空
			if (StrUtils.isNULL(skuValue2)) {
				throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "属性值2");
			}
		}
		if (StrUtils.isNotNULL(skuName1) && StrUtils.isNotNULL(skuName1)) {
			// 判断属性名是否相同
			if (skuName1.equals(skuName2)) {
				throw new ServiceException(StatusHouse.SKU_NAME_IS_SAME);
			}
		}
		// 判断属性及属性值是否已维护(正)
		ProductSkuRelation productSkuRelation1 = productSkuRelationRepository
				.findProductSkuRelationByProductIdAndSkuNameAndValue(productId, skuName1, skuValue1, skuName2,
						skuValue2, apiClientId);
		if (productSkuRelation1 != null) {
			throw new ServiceException(StatusHouse.PRDT_SKU_REL_IS_EXIST);
		}
		// 判断属性及属性值是否已维护(反)
		ProductSkuRelation productSkuRelation2 = productSkuRelationRepository
				.findProductSkuRelationByProductIdAndSkuNameAndValue(productId, skuName2, skuValue2, skuName1,
						skuValue1, apiClientId);
		if (productSkuRelation2 != null) {
			throw new ServiceException(StatusHouse.PRDT_SKU_REL_IS_EXIST);
		}

		ProductSkuRelation productSkuRelation = new ProductSkuRelation();
		productSkuRelation.setProductId(productId);
		productSkuRelation.setPrice(price);
		productSkuRelation.setSkuName1(skuName1);
		productSkuRelation.setSkuName2(skuName2);
		productSkuRelation.setSkuValue1(skuValue1);
		productSkuRelation.setSkuValue2(skuValue2);
		productSkuRelation.setStock(stock);
		productSkuRelation.setApiClientId(apiClientId);
		productSkuRelationRepository.save(productSkuRelation);

		return productSkuRelation;
	}

	@Override
	public Page<ProductSkuRelation> findProductSkuRelationPage(Integer pageNo, Integer pageSize,
			Map<String, Object> searchParams, String sortParam, String sortType, Long apiClientId) {
		return productSkuRelationRepository.findProductSkuRelationPage(pageNo, pageSize, searchParams, sortParam,
				sortType, apiClientId);
	}

	@Override
	public void deleteProductSkuRelations(String ids, Long apiClientId) {
		if (StrUtils.isNULL(ids)) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "商品属性关系id");
		}
		if (!ids.contains(",")) {
			ProductSkuRelation rel = (ProductSkuRelation) productSkuRelationRepository.findById(Long.valueOf(ids),
					apiClientId);
			if (rel == null) {
				throw new ServiceException(StatusHouse.PRDT_SKU_REL_IS_NOT_EXIST);
			}
			Product product = productRepository.findProductById(rel.getProductId(), apiClientId);
			if (product.getDisable() == 1) {
				throw new ServiceException(StatusHouse.PRDT_IS_SALLING);
			}
			productSkuRelationRepository.deleteById(Long.valueOf(ids));
		} else {
			String[] _ids = ids.split(",");
			Long[] _ids_ = new Long[_ids.length];
			for (int i = 0; i < _ids.length; i++) {
				_ids_[i] = Long.valueOf(_ids[i]);
			}
			for (int i = 0; i < _ids_.length; i++) {
				ProductSkuRelation rel = (ProductSkuRelation) productSkuRelationRepository.findById(_ids_[i],
						apiClientId);
				if (rel == null) {
					throw new ServiceException(StatusHouse.PRDT_SKU_REL_IS_NOT_EXIST);
				}
				Product product = productRepository.findProductById(rel.getProductId(), apiClientId);
				if (product.getDisable() == 1) {
					throw new ServiceException(StatusHouse.ONE_OR_MORE_PRDT_IS_SALLING);
				}
				productSkuRelationRepository.deleteById(_ids_[i]);
			}
		}
	}

	@Override
	public ProductSkuRelation updateProductSkuRelation(Long productSkuRelationId, String skuName1, String skuValue1,
			String skuName2, String skuValue2, Integer stock, Double price, Long apiClientId) {
		if (productSkuRelationId == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "商品属性关系id");
		}
		ProductSkuRelation productSkuRelation = (ProductSkuRelation) productSkuRelationRepository
				.getById(productSkuRelationId);
		if (productSkuRelation == null) {
			throw new ServiceException(StatusHouse.PRDT_SKU_REL_IS_NOT_EXIST);
		}
		Product product = productRepository.findProductById(productSkuRelation.getProductId(), apiClientId);
		if (product.getDisable() == 1) {
			throw new ServiceException(StatusHouse.PRDT_IS_SALLING);
		}
		if (StrUtils.isNotNULL(skuName1)) {
			productSkuRelation.setSkuName1(skuName1);
		}
		if (StrUtils.isNotNULL(skuValue1)) {
			productSkuRelation.setSkuValue1(skuValue1);
		}
		if (StrUtils.isNotNULL(skuName2)) {
			productSkuRelation.setSkuName2(skuName2);
		}
		if (StrUtils.isNotNULL(skuValue2)) {
			productSkuRelation.setSkuValue2(skuValue2);
		}
		if (stock != null) {
			productSkuRelation.setStock(stock);
		}
		if (price != null) {
			productSkuRelation.setPrice(price);
		}
		productSkuRelationRepository.update(productSkuRelation);

		if (productSkuRelation.getIsDefault() == 1) {
			product.setSkuName1(productSkuRelation.getSkuName1());
			product.setSkuValue1(productSkuRelation.getSkuValue1());
			product.setSkuName2(productSkuRelation.getSkuName2());
			product.setSkuValue2(productSkuRelation.getSkuValue2());
			product.setStock(productSkuRelation.getStock());
			product.setPrice(productSkuRelation.getPrice());
			productRepository.update(product);
		}

		return productSkuRelation;
	}

	@Override
	public ProductSkuRelation findProductSkuRelationById(Long productSkuRelationId, Long apiClientId) {
		return (ProductSkuRelation) productSkuRelationRepository.findById(productSkuRelationId, apiClientId);
	}

	@Override
	public List<ProductSkuRelation> findProductSkuRelationsByProductId(Long productId, Integer isDefault,
			Long apiClientId) {
		if (productId == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "商品id");
		}
		return productSkuRelationRepository.findProductSkuRelationsByProductId(productId, isDefault, apiClientId);
	}

	@Override
	public ProductSkuRelation executeSetDefault(Long productSkuRelationId, Long apiClientId) {
		if (productSkuRelationId == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "商品属性关系id");
		}
		ProductSkuRelation productSkuRelation = (ProductSkuRelation) productSkuRelationRepository
				.getById(productSkuRelationId);
		if (productSkuRelation == null) {
			throw new ServiceException(StatusHouse.PRDT_SKU_REL_IS_NOT_EXIST);
		}
		Product product = productRepository.findProductById(productSkuRelation.getProductId(), apiClientId);
		productSkuRelation.setIsDefault(1);
		productSkuRelationRepository.update(productSkuRelation);

		// 将其它的设为非默认
		List<ProductSkuRelation> rels = productSkuRelationRepository.findProductSkuRelationsByProductId(product.getId(),
				null, apiClientId);
		for (ProductSkuRelation productSkuRelation2 : rels) {
			if (productSkuRelation2.getId() != productSkuRelationId) {
				productSkuRelation2.setIsDefault(0);
				productSkuRelationRepository.update(productSkuRelation2);
			}
		}
		// 更新该商品冗余字段
		product.setProductSkuRelationId(productSkuRelation.getId());
		product.setPrice(productSkuRelation.getPrice());
		product.setSkuName1(productSkuRelation.getSkuName1());
		product.setSkuValue1(productSkuRelation.getSkuValue1());
		product.setSkuName2(productSkuRelation.getSkuName2());
		product.setSkuValue2(productSkuRelation.getSkuValue2());
		product.setStock(productSkuRelation.getStock());
		productRepository.update(product);

		return productSkuRelation;
	}

}
